package com.ybb.account.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ybb.account.entity.MerchantAccountEntity;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.Map;

@Mapper
public interface MerchantAccountMapper extends BaseMapper<MerchantAccountEntity> {

    @Update("update ybb_merchant_account set balance=balance+#{amount} where code=#{code}")
    Integer enchargeAccount(@Param("code") String code, @Param("amount") Double amount);

    @Select("SELECT code," +
            "        (SELECT balance_after FROM ybb_merchant_account_change AS sub WHERE sub.code = main.code AND sub.create_time < #{end} ORDER BY sub.create_time ASC LIMIT 1 " +
            "        )" +
            "        -" +
            "        (" +
            "            SELECT balance_before FROM ybb_merchant_account_change AS sub WHERE sub.code = main.code AND sub.create_time >= #{start} ORDER BY sub.create_time desc LIMIT 1" +
            "        ) AS balance_add" +
            "    FROM ybb_merchant_account_change AS main" +
            "    WHERE create_time >=#{start} AND create_time<#{end} GROUP BY code")
    @MapKey("code")
    Map<String, Double> settleMerchantBalance(@Param("start") Date start, @Param("end") Date end);
}
